<template>
	<view class="my-setting">
		<view class="top-tab"></view>
		<view class="setting_box">
			<view class="setting_item" @click="useHelp">
				<view class="item_img">
					<image src="../../static/city-manage/setting_help.png" mode="aspectFit"></image>
				</view>
				<view class="item_text">使用帮助</view>
			</view>
			<view class="setting_item" @click="undateVersion('click')">
				<view class="item_img">
					<image src="../../static/city-manage/setting_update.png" mode="aspectFit"></image>
				</view>
				<view class="item_text">检查更新</view>
			</view>
			<view class="setting_item">
				<view class="item_img">
					<image src="../../static/city-manage/setting_bb.png" mode="aspectFit"></image>
				</view>
				<view class="item_text">当前版本号：<view class="app_version">{{app_version}}</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import { getAppVersion } from '@/api/my_setting.js';
	import URL from '@/util/url.js'
	export default {
		data() {
			return {
				type: '',
				app_version: '', //版本号
				updateMsg: '', //检查更新信息
			}
		},
		onLoad() {
			//获取是安卓还是ios
			let system = uni.getSystemInfoSync().platform;

			if (system == 'ios') {
				this.type = "businessDisposal_ios";
			}
			if (system == 'android') {
				this.type = "businessDisposal";
			}
			//版本信息
			this.getAppVersion();
		},
		methods: {
			//获取版本信息
			getAppVersion() {
				plus.runtime.getProperty(plus.runtime.appid, (wgtinfo) => {
					this.app_version = wgtinfo.version;
				});
			},
			//检查更新
			undateVersion(clickType) {
				let appVersion = ''
				uni.getSystemInfo({
					success: function(e) {
						appVersion = e.platform
					}
				})
				let _this = this;
				//此处为访问后台是否有更新
				getAppVersion({
					type: this.type
				}).then(res => {
					//如果后台版本比当前版本高则更新
					if (res.data.app_version != this.app_version) {
						if (clickType === 'click') {
							uni.showModal({
								title: "版本更新",
								content: res.data.update_content, //更新描述
								confirmText: '立即更新',
								cancelText: '稍后进行',
								success: sucRes => {
									if (sucRes.confirm) {
										//如果是安卓直接更新下载
										if (appVersion === 'android') {
											_this.doUpData();

											//如果是ios跳转到app store
										} else {
											//在App Store Connect中的App Store下的app信息，可找到appleId
											let appleId = 123456789
											plus.runtime.launchApplication({
												action: `itms-apps://itunes.apple.com/cn/app/id${appleId}?mt=8`
											}, function(e) {
												console.log(
													'Open system default browser failed: ' +
													e.message);
											});
										}
									}
								}
							})
							//页面初始化如果有更新则显示
						} else {
							this.app_version = '发现新版本'
						}
					} else {
						if (clickType === 'click') {
							uni.showToast({
								icon: 'none',
								title: '处置系统：当前已是最新版本'
							})
						} else {
							this.app_version = `版本 ${wgtinfo.version}`
						}
					}
				})

			},
			doUpData() {
				uni.showLoading({
					title: '更新中……'
				})
				uni.downloadFile({ //执行下载
					url: URL.Setting, //下载地址
					success: data => { //下载成功
						uni.hideLoading();
						if (data.statusCode == 200) {
							uni.showModal({
								title: '',
								content: '更新成功，确定现在安装吗？',
								confirmText: '安装',
								confirmColor: '#EE8F57',
								success: function(res) {
									if (data.statusCode === 200) {
										plus.runtime.install(data.tempFilePath, {
											force: true
										}, function(res) {
											utils.showToast('更新成功，重启中');
											plus.runtime.restart();
										});
									}
								}
							});
						}
					}
				});
			},
			//使用帮助
			useHelp() {
				uni.navigateTo({
					url: '/pages/my-setting/useHelp',
				})
			}
		}
	}
</script>

<style lang="scss">
	.my-setting {
		height: 100%;
		overflow: hidden;
		display: flex;
		flex-direction: column;

		.top-tab {
			height: 60rpx;
			background: #c4534c;
		}

		.setting_box {
			width: 660rpx;
			padding: 20rpx;
			box-shadow: #b5cede 0px 0px 10px;
			background: white;
			border-radius: 30rpx;
			flex: 1;
			margin: -52rpx auto 30rpx;

			.setting_item {
				display: flex;
				align-items: center;
				line-height: 90rpx;
				border: 1px solid #ebebeb;
				border-radius: 8px;
				margin: 10px 10px 15px;

				.item_img {
					width: 60rpx;
					height: 54rpx;
					margin: 0 30rpx;

					image {
						width: 100%;
						height: 100%;
					}
				}

				.item_text {
					font-size: 30rpx;
					display: flex;

					.app_version {
						font-weight: 500;
					}
				}

				&:nth-of-type(2) {
					.item_img {
						width: 54rpx;
					}
				}
			}
		}
	}
</style>
